package com.framework.medaipalyer;



import com.framework.R;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MediaPlayerActivity extends Activity  implements
MediaPlayer.OnInfoListener, OnBufferingUpdateListener,
OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener,
SurfaceHolder.Callback,MediaPlayer.OnErrorListener {
	MediaPlayer  mMediaPlayer ;
	private SurfaceHolder holder;
	
	String TAG= "com.uusee.test";
	private SurfaceView mPreview;
	private int mVideoWidth;
	private int mVideoHeight;
	private boolean mIsVideoSizeKnown = false;
	private boolean mIsVideoReadyToBePlayed = false;
	private boolean isPlaying = false;
	private LayoutInflater  mInflater;
	private LinearLayout mLayout;
	private FrameLayout mFrame;
	ImageView image;
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 0 : 
				String url = "http://10.1.28.192:8080/2.mp4";
				playVideo(url);
				break;
			case 1:
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_media_player);
		mPreview = (SurfaceView) findViewById(R.id.surface);
		holder = mPreview.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mFrame = (FrameLayout) findViewById(R.id.frame);
		handler.sendEmptyMessageDelayed(0, 2000);
		isPlaying = true;
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		if(mMediaPlayer!=null){

		}
		
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
	}

	public void second (View view){
//		mFrame.removeView(image);
//		Intent intent = new Intent(Intent.ACTION_VIEW);
		Intent intent = new Intent();
		intent.setDataAndType(Uri.parse("http://10.1.15.27:8080/1.mp4"), "video/*");
		startActivity(intent);
	}
	public void frist (View view){
//		Intent intent = new Intent(Intent.ACTION_VIEW);
		Intent intent = new Intent();
		intent.setDataAndType(Uri.parse("http://10.1.15.27:8080/1.mp4"), "video/*");
		startActivity(intent);
//		image = new ImageView(this);
////		image.setImageResource(R.drawable.test);
//		image.setBackgroundResource(R.drawable.test);
//		FrameLayout.LayoutParams lp = new LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
//																								FrameLayout.LayoutParams.FILL_PARENT);
//		mFrame.addView(image,lp);
	}
	private void doCleanUp() {
		// TODO Auto-generated method stub
		mVideoWidth = 0;
		mVideoHeight = 0;
		mIsVideoReadyToBePlayed = false;
		mIsVideoSizeKnown = false;
	}
	private void startVideoPlayback() {
		// TODO Auto-generated method stub
		Log.v(TAG, "startVideoPlayback");
		int h = mPreview.getHeight();
		int w = mPreview.getWidth();
		boolean isCreate = holder.isCreating();
		Log.d(TAG,"H==="+h);
		Log.d(TAG,"w==="+w);
		Log.d(TAG,"create === "+isCreate);
		holder.setFixedSize(mVideoWidth, mVideoHeight);
		mMediaPlayer.start();
		isPlaying = true;
		
	}
	private void playVideo(String data) {
		Log.e(TAG,"begin playVideo ===="+data);
		doCleanUp();
		try {
			if (mMediaPlayer != null) {
				mMediaPlayer.release();
				mMediaPlayer = null;
			}
			mMediaPlayer = new MediaPlayer();
			mMediaPlayer.setDisplay(holder);
			mMediaPlayer.setDataSource(data);
			mMediaPlayer.prepareAsync();
			mMediaPlayer.setOnBufferingUpdateListener(this);
			mMediaPlayer.setOnCompletionListener(this);
			mMediaPlayer.setOnPreparedListener(this);
			mMediaPlayer.setOnVideoSizeChangedListener(this);
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		} catch (Exception e) {
			Log.e(TAG, "error: " + e.getMessage(), e);
		}
	}

	public void onBufferingUpdate(MediaPlayer arg0, int percent) {
	}

	public void onCompletion(MediaPlayer arg0) {
		Log.d(TAG, "onCompletion called");
	}

	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		Log.v(TAG, "onVideoSizeChanged called");
		Log.d(TAG, "invalid video width(" + width + ") or height(" + height
				+ ")");
		if (width == 0 || height == 0) {
			Log.e(TAG, "invalid video width(" + width + ") or height(" + height
					+ ")");
			return;
		}
		mIsVideoSizeKnown = true;
		mVideoWidth = width;
		mVideoHeight = height;
		if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
			startVideoPlayback();
		
		}
	}

	public void onPrepared(MediaPlayer mediaplayer) {
		Log.d(TAG, "onPrepared called");
		mIsVideoReadyToBePlayed = true;

		if (mIsVideoReadyToBePlayed) {
			startVideoPlayback();
		}
	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		Log.d(TAG + "info", "what====" + what + "     extra====" + extra);
		return false;
	}

	public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
		Log.d(TAG, "surfaceChanged called i="+i+"\tj="+j+"\tk="+k);

	}

	public void surfaceDestroyed(SurfaceHolder surfaceholder) {
		Log.d(TAG, "surfaceDestroyed called");
		if(mMediaPlayer!=null){
			mMediaPlayer.pause();
			Log.d(TAG,"mMediaPlayer.pause()");
		}
	}

	public void surfaceCreated(SurfaceHolder holder) {
		Log.d(TAG, "surfaceCreated called");
			if(mMediaPlayer!=null && isPlaying) 
				  mMediaPlayer.start();
	
		
	}
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		return false;
	}
}
